//
//  SRAlertViewController.swift
//  CosClub
//
//  Created by iOS on 2020/3/13.
//  Copyright © 2020 ios. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

public struct SRAlertAction {
    public var title: String?
    public var handler: (()->())?
    public init(title: String?, handler: (()->())?) {
        self.title = title
        self.handler = handler
    }
}

extension SRAlertViewController: UIViewControllerTransitioningDelegate{
    
    public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        return PresentedPartTransition(type: .present, height:CGFloat(list.count * 54
            + 86) + safeBottomHeight )
    }
    
    public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        return PresentedPartTransition(type: .dismiss, height: CGFloat(list.count * 54) + safeBottomHeight)
    }
    
}

public class SRAlertViewController: UIViewController {
    private var bag = DisposeBag()
    private var list : [SRAlertAction] = Array()
    
    public convenience init(preferredStyle: UIAlertController.Style) {
        self.init()
        self.modalPresentationStyle = .custom
        self.transitioningDelegate = self
    }
    
    public func addAction(_ action: SRAlertAction) {
        self.list.append(action)
    }
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        
        let cancelButton = UIButton("取消",fontSize: 18)
        UIImage.color( UIColor.app.white, cornerRadius: 17, stretch: true) {image in
            cancelButton.setBackgroundImage(image, for: .normal)
        }
        
        
        cancelButton.rx.tap.subscribe(onNext: { (_) in
            self.dismiss(animated: true, completion: nil)
        }).disposed(by: self.bag)
        
        view.addSubview(cancelButton)
        
        cancelButton.snp.makeConstraints { (maker) in
            maker.height.equalTo(54)
            maker.leading.trailing.equalToSuperview().inset(UISpecification.marginHor.rawValue)
            maker.bottom.equalToSuperview().inset(safeBottomHeight)
        }
        
        cancelButton.setNeedsLayout()
        cancelButton.layoutIfNeeded()
        
        let spaceV = 16
        
        let contentView = UIImageView()
        contentView.isUserInteractionEnabled = true
        UIImage.color(UIColor.app.white, cornerRadius: 17, stretch: true) {contentView.image = $0}
        
        view.addSubview(contentView)
        
        let dataSource = self.list
        
        contentView.snp.makeConstraints { (maker) in
            maker.leading.trailing.equalToSuperview().inset(UISpecification.marginHor.rawValue)
            maker.bottom.equalTo(cancelButton.snp.top).offset(-spaceV)
            maker.height.equalTo(54 * dataSource.count)
        }
        
        
        contentView.setNeedsLayout()
        contentView.layoutIfNeeded()
        
        
        var nums = 0
        
        for model in dataSource {
            
            let btn = UIButton(model.title ?? "",fontSize: 18)
            UIImage.color( UIColor.app.white, cornerRadius: 17, stretch: true) { image in
                btn.setBackgroundImage(image , for: .normal)
            }
            contentView.addSubview(btn)
            btn.rx.tap.subscribe(onNext: { [weak self] (_) in
                guard model.handler != nil else {
                    return
                }
                self?.dismiss(animated: true) {
                    model.handler!()
                }
            }).disposed(by: self.bag)
            
            btn.snp.makeConstraints { (maker) in
                maker.height.equalTo(54)
                maker.leading.trailing.equalToSuperview()
                maker.top.equalToSuperview().offset(nums * 54)
            }
            
            nums = nums + 1
            
            // 最后一个不加分割线
            if nums != dataSource.count {
                let line = UIView(type: .line)
                contentView.addSubview(line)
                
                line.snp.makeConstraints { (maker) in
                    maker.height.equalTo(0.5)
                    maker.leading.trailing.equalToSuperview()
                    maker.bottom.equalTo(btn.snp.bottom)
                }
            }
            
        }
        // Do any additional setup after loading the view.
    }
    
}
